package com.myblog.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myblog.constant.CodeType;
import com.myblog.mapper.PrivateWordMapper;
import com.myblog.mapper.UserMapper;
import com.myblog.model.PrivateWord;
import com.myblog.service.PrivateWordService;
import com.myblog.utils.DataMap;
import com.myblog.utils.StringUtil;
import com.myblog.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PrivateWordServiceImpl implements PrivateWordService {

    @Autowired
    private PrivateWordMapper privateWordMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 获取所有悄悄话
     * 前端结构：result:{publisher:“”,content:{privateWord:"",publisherDate:"",replyContent:"",id:""}}
     * @return
     */
    @Override
    public DataMap getAllPrivateWord() {
        List<PrivateWord> privateWords = privateWordMapper.selectAllPW();
        JSONArray jsonArray = new JSONArray();

        for (PrivateWord privateWord : privateWords) {
            JSONObject jsonObject = new JSONObject();

            //获取发布人的用户姓名
            String publisherName = userMapper.selectUserNameById(Integer.parseInt(privateWord.getPublisherId()));
            jsonObject.put("publisher",publisherName);

            //传入“content”内容
            JSONArray content = new JSONArray();
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("privateWord",privateWord.getPrivateWord());
            jsonObject1.put("publisherDate",privateWord.getPublisherDate());
            jsonObject1.put("replyContent",privateWord.getReplyContent());
            jsonObject1.put("id",privateWord.getId());

            content.add(jsonObject1);
            jsonObject.put("content",content);

            jsonArray.add(jsonObject);
        }

        //将结果放入result
        JSONObject result = new JSONObject();
        result.put("result",jsonArray);

        return DataMap.success().setData(result);
    }


    @Override
    public DataMap getAllPrivateWordByUserId(Integer rows,Integer pageNum,Integer id) {
        //处理分页查询
        PageHelper.startPage(pageNum,rows);
        List<PrivateWord> privateWords = privateWordMapper.selectPWByUserId(Integer.toString(id));
        PageInfo<PrivateWord> pageInfo = new PageInfo<>(privateWords);
        String publisherName = userMapper.selectUserNameById(id);

        //处理悄悄话内容信息
        JSONArray jsonArray = new JSONArray();

        for (PrivateWord privateWord : privateWords) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("publisherDate",privateWord.getPublisherDate());
            //获取发布人的用户姓名
            jsonObject.put("publisher",publisherName);
            jsonObject.put("privateWord",privateWord.getPrivateWord());
            jsonObject.put("replyContent",privateWord.getReplyContent());
            //获取回复人的名字
            String replierId = privateWord.getReplierId();
            if(replierId == null){
                jsonObject.put("replier", StringUtil.BLANK);
                jsonObject.put("replyContent",StringUtil.BLANK);
            }
            else {
                String replyName = userMapper.selectUserNameById(Integer.parseInt(privateWord.getReplierId()));
                jsonObject.put("replier",replyName);
                jsonObject.put("replyContent",privateWord.getReplyContent());
            }
            jsonObject.put("id",privateWord.getId());

            jsonArray.add(jsonObject);
        }

        //将结果放入result
        JSONObject result = new JSONObject();
        result.put("result",jsonArray);

        //将分页信息放入result
        JSONObject pageJson = new JSONObject();
        pageJson.put("pageNum",pageInfo.getPageNum());
        pageJson.put("pageSize",pageInfo.getSize());
        pageJson.put("total",pageInfo.getTotal());
        pageJson.put("pages",pageInfo.getPages());
        pageJson.put("isFirstPage",pageInfo.isIsFirstPage());
        pageJson.put("isLastPage",pageInfo.isIsLastPage());

        result.put("pageInfo",pageJson);

        return DataMap.success().setData(result);
    }

    @Override
    public DataMap sendPrivateWord(PrivateWord privateWord, Integer id) {
        if (privateWord.getPrivateWord() == null){
            return DataMap.fail();
        }
        //获取发布时间
        TimeUtil timeUtil = new TimeUtil();
        String formatDateForSix = timeUtil.getFormatDateForSix();

        privateWord.setPublisherDate(formatDateForSix);

        //设置发布人id
        privateWord.setPublisherId(Integer.toString(id));

        privateWordMapper.addPrivateWord(privateWord);

        return DataMap.success();
    }


}
